package com.task;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.TableField;
import com.pojo.MovieList;
import com.pojo.ProgramCategory;
import com.pojo.Special;
import com.pojo.SpecialFilmList;
import com.service.DyService;
import com.service.DyTypeService;
import com.service.SpecialFilmListService;
import com.service.SpecialService;
import com.util.DesUtil;
import com.util.HardUtil;
import com.util.MD5Util;
import com.util.StringUtils;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@SuppressWarnings("ALL")
@Component("TimeTaskUpdateRegistInfoService")
public class TimeTaskUpdateRegistInfoService {


    @Value("${server.port}")
    private String port;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    @Qualifier("StringRedisTemplate")
    private RedisTemplate redisTemplate;

    @Autowired
    private DyTypeService dyTypeService;

    @Autowired
    private DyService dyService;

    @Value("${server.url}")
    private String server;

    @Autowired
    private RestTemplate restTemplate;


    @Autowired
    private SpecialService specialService;

    @Autowired
    private SpecialFilmListService specialFilmListService;

    @Autowired

    @Value("${dy.tmp}")
    private String tmp;

    @Value("${dy.home}")
    private String home;


    /**
     * 每隔5分下载apk
     */
    @Scheduled(cron = "0 0/5 * * * ?")
    @PostConstruct
    public synchronized void downApk() {
        try {
            ///api/v1/apk/apkUpdate?mainBoard=47431A82
            File apkDir = new File(home + "apk");
            if (apkDir.exists() == false) {
                apkDir.mkdir();
            }
            String id = HardUtil.getMainBorId();
            Map map = new HashMap();
            map.put("mainBoradId", id); //主板序列号

            String url = server + "api/v1/apk/apkUpdate?mainBoard={1}";
            String result = restTemplate.getForObject(url, String.class, id);
            //System.out.println(result);
            JSONObject jsonObject = JSONObject.parseObject(result);

            Integer code = jsonObject.getInteger("code");
            if (code != 200) {
                return;
            }
            JSONObject dataObject = jsonObject.getJSONObject("data");
            String upgrade_url = dataObject.getString("url");
            String filename = StringUtils.getFilename(upgrade_url);
            String md5 = StringUtils.getURLParam(upgrade_url, "md5");
            String version = dataObject.getString("version");
            String forceUpgrade = dataObject.getString("forceUpgrade");
            String msg = dataObject.getString("msg");
            String md5Redis = (String) redisTemplate.boundValueOps("upgrade_md5").get();
            boolean needDown = false;
            if (md5Redis == null || md5Redis.equals(md5) == false) {
                needDown = true;
            }
            File apkFile = null;
            if (filename != null) {
                apkFile = new File(home + "apk", filename);
                if (apkFile.exists() == false) {
                    needDown = true;
                }
            }
            if (needDown && apkFile != null) {
                HttpUtil.downloadFile(upgrade_url, apkFile);
                redisTemplate.boundValueOps("upgrade_md5").set(md5);
                redisTemplate.boundValueOps("upgrade_filename").set(filename);
                redisTemplate.boundValueOps("upgrade_msg").set(msg);
                redisTemplate.boundValueOps("upgrade_version").set(version);
                redisTemplate.boundValueOps("upgrade_forceUpgrade").set(forceUpgrade);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /**
     * 更新注册信息
     */

    @Scheduled(cron = "30 * * * * ?")
    @Async
    public void updateRegistInfo() {

        try {
            String id = HardUtil.getMainBorId();
            Map map = new HashMap();
            map.put("mainBoradId", id); //主板序列号

            String url = server + "api/v1/getShouquan?mainboardId={1}";
            String result = restTemplate.getForObject(url, String.class, id);

            JSONObject jsonObject = JSONObject.parseObject(result);

            Long local_expire = jsonObject.getLong("dy_shouquan_daoqi");//到期时间
            if (local_expire == null) {
                local_expire = 0L;
            }
            Integer local_max = jsonObject.getInteger("dy_max_shouquan");
            if (local_max == null) {
                local_max = 0;
            }

            //版权授权图片
            String copyright_image = jsonObject.getString("copyright_image");

            //版权logo
            String copyright_logo = jsonObject.getString("copyright_logo");

            //欢迎界面
            String welcomeImg = jsonObject.getString("welcomeImg");

            //电影服务器
            String dy_down_ip = jsonObject.getString("dy_down_ip"); //电影下载ip

            String dy_down_port = jsonObject.getString("dy_down_port");//电影下载端口

            String dy_down_status = jsonObject.getString("dy_down_status");//电影下载状态 1启用，0禁用


            //在线获取参数
            redisTemplate.boundValueOps("local_expire").set(local_expire);
            redisTemplate.boundValueOps("local_max").set(local_max);
            redisTemplate.boundValueOps("copyright_image").set(copyright_image);

            redisTemplate.boundValueOps("dy_down_ip").set(dy_down_ip);
            redisTemplate.boundValueOps("dy_down_port").set(dy_down_port);
            redisTemplate.boundValueOps("dy_down_status").set(dy_down_status);

            redisTemplate.boundValueOps("copyright_logo").set(copyright_logo);
            redisTemplate.boundValueOps("welcomeImg").set(welcomeImg);


        } catch (Exception e) {
            //e.printStackTrace();
        }


    }


}
